package com.hsjk.szwj.module.train.controller.admin;

import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.module.train.controller.admin.vo.NewsCreateReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.NewsPageReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.NewsRespVO;
import com.hsjk.szwj.module.train.controller.admin.vo.NewsUpdateReqVO;
import com.hsjk.szwj.module.train.convert.NewsConvert;
import com.hsjk.szwj.module.train.dal.dataobject.news.NewsDO;
import com.hsjk.szwj.module.train.service.NewsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;

import com.hsjk.szwj.framework.common.pojo.CommonResult;
import static com.hsjk.szwj.framework.common.pojo.CommonResult.success;




@Tag(name = "管理后台 - 驾校新闻")
@RestController
@RequestMapping("/train/news")
@Validated
public class NewsController {

    @Resource
    private NewsService newsService;

    @PostMapping("/create")
    @Operation(summary = "创建驾校新闻")
    public CommonResult<Integer> createNews(@Valid @RequestBody NewsCreateReqVO createReqVO) {
        return success(newsService.createNews(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新驾校新闻")
    public CommonResult<Boolean> updateNews(@Valid @RequestBody NewsUpdateReqVO updateReqVO) {
        newsService.updateNews(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除驾校新闻")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteNews(@RequestParam("id") Integer id) {
        newsService.deleteNews(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得驾校新闻")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<NewsRespVO> getNews(@RequestParam("id") Integer id) {
        NewsDO news = newsService.getNews(id);
        return success(NewsConvert.INSTANCE.convert(news));
    }

    @GetMapping("/page")
    @Operation(summary = "获得驾校新闻分页")
    public CommonResult<PageResult<NewsRespVO>> getNewsPage(@Valid NewsPageReqVO pageVO) {
        PageResult<NewsDO> pageResult = newsService.getNewsPage(pageVO);
        return success(NewsConvert.INSTANCE.convertPage(pageResult));
    }


}
